<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="地区" prop="dealerAddress">
        <CityPicker ref="cityPicker" size="small" clearable placeholder="请选择" v-model="queryParams.dealerAddress" />
      </el-form-item>
      <el-form-item label="经销商类别" prop="dealerType">
        <el-select v-model="queryParams.dealerType" placeholder="请选择" clearable size="small">
          <el-option v-for="type in dealerTypeOptions" :key="type.value" :label="type.label" :value="type.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="经销商" prop="dealerName">
        <el-input v-model="queryParams.dealerName" placeholder="请输入经销商名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['gongyuan:dealer:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="经销商编号" prop="dealerNumber" :show-overflow-tooltip="true" />
      <el-table-column label="经销商名称" prop="dealerName" :show-overflow-tooltip="true" />
      <el-table-column label="负责人" prop="dealerUserName" :show-overflow-tooltip="true" />
      <el-table-column label="联系电话" prop="dealerPhone" :show-overflow-tooltip="true" />
      <el-table-column label="所在省市区" prop="dealerAddress" :show-overflow-tooltip="true" />
      <el-table-column label="运营情况" align="center" :show-overflow-tooltip="true" :formatter="statusFormatter" />
      <el-table-column label="分配权重" align="center" prop="dealerProportion" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleDetail(scope.row)" v-hasPermi="['gongyuan:dealer:byid']">详情</el-button>
          <el-button class="gongyuan-update" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['gongyuan:dealer:edit']">修改</el-button>
          <el-button class="gongyuan-delete" v-if="scope.row.dealerId!=1" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['gongyuan:dealer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import CityPicker from '@/components/cityPicker'
import { getDealerList, delDealer } from '@/api/dealer/index'
export default {
  name: 'Dealerlist',
  components: {
    CityPicker
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      list: [],
      total: 0,
      // 查询参数
      queryParams: {
        dealerAddress: [],
        dealerName: '',
        dealerType: undefined,
        pageNum: 1,
        pageSize: 10
      },
      dealerTypeOptions: [],
      dealerStatusOptions: []
    }
  },
  created() {
    this.initDictsOptions('dealer_type', 'dealerTypeOptions')
    this.initDictsOptions('dealer_status', 'dealerStatusOptions')
    this.getList()
  },
  methods: {
    async initDictsOptions(dicts, listProps) {
      try {
        const res = await this.getDicts(dicts)
        if (res.code === 200) {
          this[listProps] = res.data.map((per) => ({
            label: per.dictLabel,
            value: per.dictValue
          }))
        }
      } catch (error) {
        console.log(error)
      }
    },
    statusFormatter(row) {
      const obj = this.dealerStatusOptions.find((item) => item.value === row.dealerStatus) || { label: '' }
      return obj.label
    },
    async getList() {
      this.loading = true
      let cityObj
      if (!this || !this.$refs.cityPicker) {
        cityObj = {
          province: '',
          city: '',
          area: ''
        }
      } else {
        cityObj = this.$refs.cityPicker.getValueTextObj(this.queryParams.dealerAddress)
      }
      const res = await getDealerList({
        ...this.queryParams,
        dealerAddress: `${cityObj.province}${cityObj.city}${cityObj.area}`
      })
      this.list = res.rows
      this.total = res.total
      this.loading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    handleAdd() {
      // 此处后台管理添加的路由, path对应组件name须一致,不可再修改
      this.$router.push({ name: 'Dealeredit' })
    },

    handleDetail(row) {
      this.$router.push({ name: 'Dealerdetail', query: { id: row.dealerId } })
    },
    handleEdit(row) {
      this.$router.push({ name: 'Dealeredit', query: { id: row.dealerId } })
    },
    handleDelete(row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delDealer(row.dealerId)
        })
        .then(() => {
          this.queryParams.pageNum = 1
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    }
  }
}
</script>
